
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>函数 · Dart编程指南</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="arcticfox">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="进阶篇.html" />
    
    
    <link rel="prev" href="简单语法.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://www.bczl.xyz/" target="_blank" class="custom-link">主页</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="基础篇.html">
            
                <a href="基础篇.html">
            
                    
                    基础篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="环境搭建.html">
            
                <a href="环境搭建.html">
            
                    
                    环境搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="简单语法.html">
            
                <a href="简单语法.html">
            
                    
                    简单语法
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.3" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="进阶篇.html">
            
                <a href="进阶篇.html">
            
                    
                    进阶篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="面向对象.html">
            
                <a href="面向对象.html">
            
                    
                    面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="泛型.html">
            
                <a href="泛型.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="枚举.html">
            
                <a href="枚举.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="库与导入.html">
            
                <a href="库与导入.html">
            
                    
                    库与导入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="异步编程.html">
            
                <a href="异步编程.html">
            
                    
                    异步编程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.5.1" data-path="异步编程之Future.html">
            
                <a href="异步编程之Future.html">
            
                    
                    Future
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5.2" data-path="异步编程之Stream.html">
            
                <a href="异步编程之Stream.html">
            
                    
                    Stream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5.3" data-path="异步编程之Isolate.html">
            
                <a href="异步编程之Isolate.html">
            
                    
                    Isolate
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="异常处理.html">
            
                <a href="异常处理.html">
            
                    
                    异常处理与 Zone
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="生成器.html">
            
                <a href="生成器.html">
            
                    
                    生成器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="标准流与文件.html">
            
                <a href="标准流与文件.html">
            
                    
                    标准流与文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="运算符.html">
            
                <a href="运算符.html">
            
                    
                    附：运算符
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="常用标准库.html">
            
                <a href="常用标准库.html">
            
                    
                    常用标准库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="字符串.html">
            
                <a href="字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="集合.html">
            
                <a href="集合.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="URI处理.html">
            
                <a href="URI处理.html">
            
                    
                    URI处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="日期和时间.html">
            
                <a href="日期和时间.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="环境及外部调用.html">
            
                <a href="环境及外部调用.html">
            
                    
                    环境及外部调用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="其他.html">
            
                <a href="其他.html">
            
                    
                    其他
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="实战篇.html">
            
                <a href="实战篇.html">
            
                    
                    实战篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="网络编程.html">
            
                <a href="网络编程.html">
            
                    
                    网络编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="爬虫实战.html">
            
                <a href="爬虫实战.html">
            
                    
                    爬虫实战
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="HTTP服务器.html">
            
                <a href="HTTP服务器.html">
            
                    
                    HTTP服务器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="WebSocket服务器.html">
            
                <a href="WebSocket服务器.html">
            
                    
                    WebSocket服务器
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    附录
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="dart2.14.html">
            
                <a href="dart2.14.html">
            
                    
                    升级到Dart 2.14
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="http://www.bczl.xyz/" target="blank" class="gitbook-link">
            编程之路
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >函数</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><ul><li><span class="title-icon "></span><a href="#&#x51FD;&#x6570;"><b></b>&#x51FD;&#x6570;</a></li><ul><li><span class="title-icon "></span><a href="#&#x5B9A;&#x4E49;&#x51FD;&#x6570;"><b></b>&#x5B9A;&#x4E49;&#x51FD;&#x6570;</a></li><li><span class="title-icon "></span><a href="#&#x533F;&#x540D;&#x51FD;&#x6570;"><b></b>&#x533F;&#x540D;&#x51FD;&#x6570;</a></li><li><span class="title-icon "></span><a href="#&#x7BAD;&#x5934;&#x51FD;&#x6570;"><b></b>&#x7BAD;&#x5934;&#x51FD;&#x6570;</a></li><li><span class="title-icon "></span><a href="#&#x51FD;&#x6570;&#x7C7B;&#x578B;&#x4E0E;&#x51FD;&#x6570;&#x7B7E;&#x540D;"><b></b>&#x51FD;&#x6570;&#x7C7B;&#x578B;&#x4E0E;&#x51FD;&#x6570;&#x7B7E;&#x540D;</a></li></ul></ul></ul></div><a href="#" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><!-- ex_nolevel -->
<h2 id="&#x51FD;&#x6570;"><a name="&#x51FD;&#x6570;" class="anchor-navigation-ex-anchor" href="#&#x51FD;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x51FD;&#x6570;</h2>
<blockquote>
<p>&#x5728;Dart&#x4E2D;&#xFF0C;&#x51FD;&#x6570;&#xFF08;&#x6216;&#x65B9;&#x6CD5;&#xFF09; &#x4E5F;&#x662F;&#x5BF9;&#x8C61;&#xFF0C;&#x5B83;&#x7684;&#x7C7B;&#x578B;&#x662F; <code>Function</code>&#x3002; &#x8FD9;&#x610F;&#x5473;&#x7740;&#xFF0C;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x8D4B;&#x503C;&#x7ED9;&#x53D8;&#x91CF;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x5F53;&#x505A;&#x5176;&#x4ED6;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x3002;</p>
</blockquote>
<h3 id="&#x5B9A;&#x4E49;&#x51FD;&#x6570;"><a name="&#x5B9A;&#x4E49;&#x51FD;&#x6570;" class="anchor-navigation-ex-anchor" href="#&#x5B9A;&#x4E49;&#x51FD;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5B9A;&#x4E49;&#x51FD;&#x6570;</h3>
<p>Dart&#x4E2D;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#xFF0C;&#x57FA;&#x672C;&#x4E0A;&#x4E0E;Java&#x7C7B;&#x4F3C;</p>
<pre><code class="lang-dart"><span class="hljs-built_in">String</span> greet(<span class="hljs-built_in">String</span> name){
    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;hello,$name&quot;</span>;
}
</code></pre>
<p>&#x5728;Dart&#x4E2D;&#xFF0C;&#x7C7B;&#x578B;&#x662F;&#x53EF;&#x9009;&#xFF0C;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#x663E;&#x5F0F;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x4F46;&#x4ECD;&#x7136;&#x5EFA;&#x8BAE;&#x663E;&#x5F0F;&#x6307;&#x5B9A;&#x7C7B;&#x578B;&#x3002;</p>
<pre><code class="lang-dart">greet(name){
    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;hello,$name&quot;</span>;
}
</code></pre>
<p>&#x8981;&#x6CE8;&#x610F;&#xFF0C;&#x51FD;&#x6570;&#x4E5F;&#x662F;&#x5BF9;&#x8C61;&#xFF0C;&#x6240;&#x6709;&#x51FD;&#x6570;&#x90FD;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#x3002;&#x5F53;&#x6CA1;&#x6709;&#x6307;&#x5B9A;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x51FD;&#x6570;&#x4F1A;&#x8FD4;&#x56DE;<code>null</code>&#x3002;&#x5F53;&#x7136;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x5F3A;&#x884C;&#x4F7F;&#x7528;<code>void</code>&#x6765;&#x4FEE;&#x9970;&#x51FD;&#x6570;&#xFF0C;&#x5219;&#x51FD;&#x6570;&#x771F;&#x7684;&#x6CA1;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x5C31;&#x53E6;&#x5F53;&#x522B;&#x8BBA;&#x4E86;&#x3002;</p>
<h4 id="&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;"><a name="&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;" class="anchor-navigation-ex-anchor" href="#&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;</h4>
<p>Dart&#x4E2D;&#x652F;&#x6301;&#x4E24;&#x79CD;&#x53EF;&#x9009;&#x53C2;&#x6570;</p>
<ul>
<li>&#x547D;&#x540D;&#x53EF;&#x9009;&#x53C2;&#x6570;</li>
<li>&#x4F4D;&#x7F6E;&#x53EF;&#x9009;&#x53C2;&#x6570;</li>
</ul>
<p>&#x5728;Java&#x4E2D;&#x901A;&#x5E38;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;&#x91CD;&#x8F7D;&#x6765;&#x5B9E;&#x73B0;&#x540C;&#x540D;&#x65B9;&#x6CD5;&#x7684;&#x4E0D;&#x540C;&#x53C2;&#x6570;&#x8C03;&#x7528;&#xFF0C;Dart&#x4E2D;&#x5219;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x53C2;&#x6570;&#x6765;&#x5B9E;&#x73B0;&#x76F8;&#x540C;&#x6548;&#x679C;&#x3002;</p>
<h4 id="&#x547D;&#x540D;&#x53EF;&#x9009;&#x53C2;&#x6570;"><a name="&#x547D;&#x540D;&#x53EF;&#x9009;&#x53C2;&#x6570;" class="anchor-navigation-ex-anchor" href="#&#x547D;&#x540D;&#x53EF;&#x9009;&#x53C2;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x547D;&#x540D;&#x53EF;&#x9009;&#x53C2;&#x6570;</h4>
<p>&#x5148;&#x6765;&#x770B;&#x4E00;&#x4E0B;<code>&#x547D;&#x540D;&#x53C2;&#x6570;</code>&#xFF0C;&#x5B83;&#x4F7F;&#x7528;&#x82B1;&#x62EC;&#x53F7;&#x6765;&#x5B9A;&#x4E49;&#x53C2;&#x6570;&#x5217;&#x8868;</p>
<pre><code class="lang-dart"><span class="hljs-comment">// &#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x53C2;&#x6570;&#x5217;&#x8868;&#x7528;&#x82B1;&#x62EC;&#x53F7;&#x5305;&#x88F9;</span>
enableFlags({<span class="hljs-built_in">bool</span> bold, <span class="hljs-built_in">bool</span> hidden}) {
    <span class="hljs-comment">// do something</span>
}

<span class="hljs-comment">// &#x8C03;&#x7528;&#x65B9;&#x5F0F;&#xFF0C;&#x4F20;&#x53C2;&#x65F6;&#x4F7F;&#x7528;&quot;&#x53C2;&#x6570;&#x540D;:&#x503C;&quot;&#x7684;&#x5F62;&#x5F0F;</span>
enableFlags(hidden:<span class="hljs-keyword">true</span>,bold:<span class="hljs-keyword">false</span>);
</code></pre>
<p>&#x5982;&#x679C;&#x5728;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x65F6;&#xFF0C;&#x7ED9;&#x53C2;&#x6570;&#x5217;&#x8868;&#x4E2D;&#x7684;&#x53C2;&#x6570;&#x8BBE;&#x7F6E;&#x9ED8;&#x8BA4;&#x503C;&#xFF0C;&#x5219;&#x8BE5;&#x53C2;&#x6570;&#x5C31;&#x662F;&#x53EF;&#x9009;&#x7684;&#xFF0C;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x65F6;&#x53EF;&#x4EE5;&#x5FFD;&#x7565;&#x8BE5;&#x53C2;&#x6570;&#xFF0C;&#x4F7F;&#x7528;&#x9ED8;&#x8BA4;&#x7684;&#x503C;&#x3002;</p>
<pre><code class="lang-dart"><span class="hljs-comment">// &#x5B9A;&#x4E49;add&#x51FD;&#x6570;</span>
add({<span class="hljs-built_in">int</span> x, <span class="hljs-built_in">int</span> y=<span class="hljs-number">1</span>, <span class="hljs-built_in">int</span> z=<span class="hljs-number">0</span>}){
    <span class="hljs-built_in">print</span>(x + y + z;
}

<span class="hljs-comment">// &#x8C03;&#x7528;</span>
add(x:<span class="hljs-number">18</span>);              <span class="hljs-comment">// 19</span>
add(x:<span class="hljs-number">18</span>, y:<span class="hljs-number">2</span>, z:<span class="hljs-number">10</span>);   <span class="hljs-comment">// 30</span>
</code></pre>
<p>&#x8FD9;&#x91CC;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x4E00;&#x4E0B;&#xFF0C;SDK 1.21&#x4E4B;&#x524D;&#x7684;&#x7248;&#x672C;&#x4E2D;&#xFF0C;&#x547D;&#x540D;&#x53C2;&#x6570;&#x4E0D;&#x80FD;&#x4F7F;&#x7528;<code>=</code>&#x53F7;&#x6765;&#x8BBE;&#x7F6E;&#x9ED8;&#x8BA4;&#x503C;&#xFF0C;&#x800C;SDK 1.21&#x4E4B;&#x540E;&#xFF0C;&#x53EA;&#x80FD;&#x4F7F;&#x7528;<code>=</code>&#x53F7;&#x6765;&#x8BBE;&#x7F6E;&#x9ED8;&#x8BA4;&#x503C;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x8BF7;&#x68C0;&#x67E5;&#x5E76;&#x5347;&#x7EA7;SDK&#x7248;&#x672C;&#x3002;</p>
<h4 id="&#x4F4D;&#x7F6E;&#x53EF;&#x9009;&#x53C2;&#x6570;"><a name="&#x4F4D;&#x7F6E;&#x53EF;&#x9009;&#x53C2;&#x6570;" class="anchor-navigation-ex-anchor" href="#&#x4F4D;&#x7F6E;&#x53EF;&#x9009;&#x53C2;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4F4D;&#x7F6E;&#x53EF;&#x9009;&#x53C2;&#x6570;</h4>
<p><code>&#x4F4D;&#x7F6E;&#x53EF;&#x9009;&#x53C2;&#x6570;</code>&#x4F7F;&#x7528;&#x4E2D;&#x62EC;&#x53F7;&#x6765;&#x5B9A;&#x4E49;&#x53C2;&#x6570;&#x5217;&#x8868;&#xFF0C;&#x4E2D;&#x62EC;&#x53F7;&#x4E2D;&#x7684;&#x53C2;&#x6570;&#x662F;&#x53EF;&#x9009;&#x7684;</p>
<pre><code class="lang-dart"><span class="hljs-comment">// &#x5B9A;&#x4E49;add&#x51FD;&#x6570;</span>
add(<span class="hljs-built_in">int</span> x, [<span class="hljs-built_in">int</span> y, <span class="hljs-built_in">int</span> z]){
    <span class="hljs-built_in">int</span> result = x;
    <span class="hljs-keyword">if</span> (y !=  <span class="hljs-keyword">null</span>){
        result = result + y;
    }

    <span class="hljs-keyword">if</span> (z !=  <span class="hljs-keyword">null</span>){
        result = result + z;
    }
    <span class="hljs-built_in">print</span>(result);
}

<span class="hljs-comment">// &#x8C03;&#x7528;</span>
add(<span class="hljs-number">18</span>);           <span class="hljs-comment">// 18</span>
add(<span class="hljs-number">18</span>,<span class="hljs-number">12</span>);        <span class="hljs-comment">// 30</span>
add(<span class="hljs-number">18</span>, <span class="hljs-number">12</span>, <span class="hljs-number">15</span>);   <span class="hljs-comment">// 45</span>
</code></pre>
<p>&#x7ED9;<code>&#x4F4D;&#x7F6E;&#x53EF;&#x9009;&#x53C2;&#x6570;</code>&#x8BBE;&#x7F6E;&#x9ED8;&#x8BA4;&#x503C;</p>
<pre><code class="lang-dart"><span class="hljs-comment">// &#x5B9A;&#x4E49;add&#x51FD;&#x6570;</span>
add(<span class="hljs-built_in">int</span> x, [<span class="hljs-built_in">int</span> y=<span class="hljs-number">0</span>, <span class="hljs-built_in">int</span> z=<span class="hljs-number">0</span>]){
    <span class="hljs-built_in">print</span>(x +&#xFF59;&#xFF0B;&#xFF5A;);
}
</code></pre>
<p>&#x6700;&#x540E;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x4E00;&#x4E0B;<code>&#x547D;&#x540D;&#x53EF;&#x9009;&#x53C2;&#x6570;</code>&#x4E0E;<code>&#x4F4D;&#x7F6E;&#x53EF;&#x9009;&#x53C2;&#x6570;</code>&#x7684;&#x533A;&#x522B;&#xFF0C;&#x524D;&#x8005;&#x4E2D;&#x7684;&#x53C2;&#x6570;&#x4E0E;&#x987A;&#x5E8F;&#x65E0;&#x5173;&#xFF0C;&#x65E0;&#x9700;&#x6309;&#x987A;&#x5E8F;&#x4F20;&#x53C2;&#xFF0C;&#x4E14;&#x4F20;&#x53C2;&#x6570;&#x65F6;&#x9700;&#x4F7F;&#x7528;&#x5192;&#x53F7;&#xFF1B;&#x540E;&#x8005;&#x4E0E;&#x987A;&#x5E8F;&#x76F8;&#x5173;&#xFF0C;&#x4F20;&#x53C2;&#x5FC5;&#x987B;&#x4F9D;&#x7167;&#x987A;&#x5E8F;&#x3002;</p>
<h3 id="&#x533F;&#x540D;&#x51FD;&#x6570;"><a name="&#x533F;&#x540D;&#x51FD;&#x6570;" class="anchor-navigation-ex-anchor" href="#&#x533F;&#x540D;&#x51FD;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x533F;&#x540D;&#x51FD;&#x6570;</h3>
<blockquote>
<p>&#x5927;&#x90E8;&#x5206;&#x51FD;&#x6570;&#x90FD;&#x6709;&#x540D;&#x5B57;&#xFF0C;&#x4F46;&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x6CA1;&#x6709;&#x540D;&#x5B57;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x79F0;&#x4E3A;&#x533F;&#x540D;&#x51FD;&#x6570;&#xFF0C;&#x4E5F;&#x88AB;&#x79F0;&#x4E3A;lambda&#x8868;&#x8FBE;&#x5F0F;&#x6216;&#x8005;&#x95ED;&#x5305;&#x3002;</p>
</blockquote>
<pre><code class="lang-dart"><span class="hljs-comment">// &#x5B9A;&#x4E49;&#x533F;&#x540D;&#x51FD;&#x6570;&#xFF0C;&#x5E76;&#x5C06;&#x5176;&#x8D4B;&#x503C;&#x7ED9;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;func&#xFF0C;&#x6CE8;&#x610F;&#xFF0C;&#x51FD;&#x6570;&#x4F53;&#x6700;&#x540E;&#x7684;&#x82B1;&#x62EC;&#x53F7;&#x5904;&#x5FC5;&#x987B;&#x6709;&#x5206;&#x53F7;&#x7ED3;&#x675F;&#x3002;</span>
<span class="hljs-keyword">var</span> func = (x,y){
    <span class="hljs-keyword">return</span> x + y;
};

<span class="hljs-built_in">print</span>(func(<span class="hljs-number">10</span>,<span class="hljs-number">11</span>));    <span class="hljs-comment">// 21</span>
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x533F;&#x540D;&#x51FD;&#x6570;&#x4E0E;&#x666E;&#x901A;&#x51FD;&#x6570;&#x57FA;&#x672C;&#x76F8;&#x540C;&#xFF0C;&#x4E5F;&#x6709;&#x53C2;&#x6570;&#x5217;&#x8868;&#xFF0C;&#x51FD;&#x6570;&#x4F53;&#xFF0C;&#x53EA;&#x662F;&#x7701;&#x53BB;&#x4E86;&#x51FD;&#x6570;&#x540D;&#x800C;&#x5DF2;&#x3002;</p>
<h3 id="&#x7BAD;&#x5934;&#x51FD;&#x6570;"><a name="&#x7BAD;&#x5934;&#x51FD;&#x6570;" class="anchor-navigation-ex-anchor" href="#&#x7BAD;&#x5934;&#x51FD;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x7BAD;&#x5934;&#x51FD;&#x6570;</h3>
<blockquote>
<p>Dart&#x4E2D;&#x7684;&#x7BAD;&#x5934;&#x51FD;&#x6570;&#x4E0E;JavaScript&#x4E2D;&#x7684;&#x57FA;&#x672C;&#x76F8;&#x540C;&#x3002;&#x5F53;&#x51FD;&#x6570;&#x4F53;&#x4E2D;&#x53EA;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x8BED;&#x53E5;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>=&gt;</code>&#x7BAD;&#x5934;&#x8BED;&#x6CD5;&#x8FDB;&#x884C;&#x7F29;&#x5199;&#x3002;&#x6CE8;&#x610F;&#xFF0C;&#x7BAD;&#x5934;&#x51FD;&#x6570;&#x4EC5;&#x4EC5;&#x53EA;&#x662F;&#x4E00;&#x4E2A;&#x7B80;&#x6D01;&#x8868;&#x8FBE;&#x7684;&#x8BED;&#x6CD5;&#x7CD6;&#x3002;</p>
</blockquote>
<p>&#x666E;&#x901A;&#x51FD;&#x6570;</p>
<pre><code class="lang-dart">add(<span class="hljs-built_in">num</span> x, <span class="hljs-built_in">num</span> y){
    <span class="hljs-keyword">return</span> x + y;
}

<span class="hljs-built_in">print</span>(add(<span class="hljs-number">18</span>,<span class="hljs-number">12</span>));    <span class="hljs-comment">// 30</span>
</code></pre>
<p>&#x7BAD;&#x5934;&#x51FD;&#x6570;</p>
<pre><code class="lang-dart"><span class="hljs-comment">// &#x4E0E;&#x4E0A;&#x9762;&#x7684;&#x666E;&#x901A;&#x51FD;&#x6570;&#x5B8C;&#x5168;&#x7B49;&#x4EF7;</span>
add(<span class="hljs-built_in">num</span> x, <span class="hljs-built_in">num</span> y) =&gt; x + y;

<span class="hljs-built_in">print</span>(add(<span class="hljs-number">18</span>,<span class="hljs-number">12</span>));    <span class="hljs-comment">// 30</span>
</code></pre>
<p>&#x7BAD;&#x5934;&#x51FD;&#x6570;&#x7701;&#x7565;&#x4E86;&#x82B1;&#x62EC;&#x53F7;&#x7684;&#x8868;&#x8FBE;&#xFF0C;&#x7BAD;&#x5934;&#x540E;&#x9762;&#x8DDF;&#x4E00;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x4E5F;&#x5C31;&#x662F;&#x8FD9;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#x3002;&#x53E6;&#x5916;&#xFF0C;&#x7BAD;&#x5934;&#x51FD;&#x6570;&#x4E5F;&#x53EF;&#x4EE5;&#x4E0E;&#x533F;&#x540D;&#x51FD;&#x6570;&#x7ED3;&#x5408;&#xFF0C;&#x5F62;&#x6210;&#x533F;&#x540D;&#x7BAD;&#x5934;&#x51FD;&#x6570;&#x3002;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">var</span> func = (<span class="hljs-built_in">num</span> x, <span class="hljs-built_in">num</span> y) =&gt; x + y;
</code></pre>
<h3 id="&#x51FD;&#x6570;&#x7C7B;&#x578B;&#x4E0E;&#x51FD;&#x6570;&#x7B7E;&#x540D;"><a name="&#x51FD;&#x6570;&#x7C7B;&#x578B;&#x4E0E;&#x51FD;&#x6570;&#x7B7E;&#x540D;" class="anchor-navigation-ex-anchor" href="#&#x51FD;&#x6570;&#x7C7B;&#x578B;&#x4E0E;&#x51FD;&#x6570;&#x7B7E;&#x540D;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x51FD;&#x6570;&#x7C7B;&#x578B;&#x4E0E;&#x51FD;&#x6570;&#x7B7E;&#x540D;</h3>
<pre><code class="lang-dart"><span class="hljs-keyword">void</span> func(<span class="hljs-built_in">Function</span> f,<span class="hljs-built_in">String</span> content){
  <span class="hljs-comment">// &#x8C03;&#x7528;f&#x51FD;&#x6570;</span>
  f(content);
}

<span class="hljs-keyword">void</span> main() {
  <span class="hljs-comment">// &#x51FD;&#x6570;&#x672C;&#x8EAB;&#x4E5F;&#x662F;&#x4E00;&#x79CD;&#x7C7B;&#x578B;&#xFF0C;&#x53EF;&#x4F7F;&#x7528;Function&#x58F0;&#x660E;&#x51FD;&#x6570;&#x7C7B;&#x578B;&#x53D8;&#x91CF;</span>
  <span class="hljs-built_in">Function</span> f = (<span class="hljs-built_in">String</span> text){
    <span class="hljs-built_in">print</span>(text);
  };

  <span class="hljs-comment">// &#x5C06;&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x4F20;&#x9012;</span>
  func(f, <span class="hljs-string">&quot;&#x7F16;&#x7A0B;&#x4E4B;&#x8DEF;&quot;</span>);
}
</code></pre>
<p>&#x5982;&#x4E0A;&#x4F8B;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x58F0;&#x660E;&#x51FD;&#x6570;&#x7C7B;&#x578B;&#x53D8;&#x91CF;&#xFF0C;&#x5E76;&#x5C06;&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x4F20;&#x9012;&#xFF0C;&#x4F46;&#x5F88;&#x5FEB;&#x6211;&#x4EEC;&#x5C31;&#x4F1A;&#x53D1;&#x73B0;&#x8FD9;&#x6837;&#x4F7F;&#x7528;&#x7684;&#x5F0A;&#x7AEF;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">void</span> main() {
  <span class="hljs-comment">// &#x5F53;&#x6211;&#x4EEC;&#x4F20;&#x5165;&#x4E00;&#x4E2A;&#x4E0E;&#x53C2;&#x6570;&#x4E0D;&#x5339;&#x914D;&#x7684;&#x51FD;&#x6570;&#x65F6;&#xFF0C;&#x8BED;&#x6CD5;&#x68C0;&#x67E5;&#x4E0D;&#x4F1A;&#x62A5;&#x9519;&#xFF0C;&#x4F46;&#x8FD0;&#x884C;&#x65F6;&#x4F1A;&#x62A5;&#x9519;</span>
  func((<span class="hljs-built_in">int</span> a,<span class="hljs-built_in">int</span> b){
    <span class="hljs-keyword">return</span> a+b;
  }, <span class="hljs-string">&quot;&#x7F16;&#x7A0B;&#x4E4B;&#x8DEF;&quot;</span>);
}
</code></pre>
<p>&#x76F4;&#x63A5;&#x4F7F;&#x7528;<code>Function</code>&#x7C7B;&#x578B;&#x6765;&#x58F0;&#x660E;&#x51FD;&#x6570;&#x53D8;&#x91CF;&#x505A;&#x5F62;&#x5F0F;&#x53C2;&#x6570;&#xFF0C;&#x662F;&#x65E0;&#x6CD5;&#x68C0;&#x67E5;&#x8BE5;&#x51FD;&#x6570;&#x7684;&#x5F62;&#x5F0F;&#x53C2;&#x6570;&#x7684;&#xFF0C;&#x800C;&#x4E14;&#x8FD9;&#x6837;&#x4EE3;&#x7801;&#x53EF;&#x8BFB;&#x6027;&#x592A;&#x5DEE;&#xFF0C;&#x6211;&#x4EEC;&#x65E0;&#x6CD5;&#x83B7;&#x77E5;&#x8BE5;&#x51FD;&#x6570;&#x53D8;&#x91CF;&#x7684;&#x5F62;&#x5F0F;&#x53C2;&#x6570;&#x58F0;&#x660E;&#x4FE1;&#x606F;&#x3002;&#x8FD9;&#x65F6;&#x5019;&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x5173;&#x952E;&#x5B57;<code>typedef</code>&#x6765;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x7684;&#x7B7E;&#x540D;&#x3002;</p>
<pre><code class="lang-dart"><span class="hljs-comment">// &#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7B7E;&#x540D;&#xFF1A;&#x8868;&#x793A;F&#x51FD;&#x6570;&#x7684;&#x539F;&#x578B;&#x4E3A; void F(String)</span>
<span class="hljs-comment">// &#x5176;&#x4E2D;F&#x8868;&#x793A;&#x8BE5;&#x51FD;&#x6570;&#x7C7B;&#x578B;&#x7684;&#x522B;&#x540D;</span>
<span class="hljs-keyword">typedef</span> <span class="hljs-keyword">void</span> F(<span class="hljs-built_in">String</span>);

<span class="hljs-comment">// &#x4F7F;&#x7528;F&#x7C7B;&#x578B;&#x6765;&#x58F0;&#x660E;&#x5F62;&#x53C2; f</span>
<span class="hljs-keyword">void</span> func(F f,<span class="hljs-built_in">String</span> content){
  f(content);
}

<span class="hljs-keyword">void</span> main() {
  <span class="hljs-comment">// &#x6B64;&#x65F6;&#x518D;&#x4F20;&#x5165;&#x4E00;&#x4E2A;&#x7B7E;&#x540D;&#x4E0D;&#x5339;&#x914D;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x8BED;&#x6CD5;&#x68C0;&#x67E5;&#x76F4;&#x63A5;&#x62A5;&#x9519;</span>
  func((<span class="hljs-built_in">int</span> a,<span class="hljs-built_in">int</span> b){
    <span class="hljs-keyword">return</span> a+b;
  }, 
}
</code></pre>
<p>&#x4F7F;&#x7528;<code>typedef</code>&#x6765;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x7684;&#x7B7E;&#x540D;&#x901A;&#x5E38;&#x6709;&#x4E24;&#x79CD;&#x5199;&#x6CD5;&#xFF0C;&#x4E00;&#x79CD;&#x5982;&#x4E0A;&#x4F8B;&#xFF0C;<code>typedef</code>&#x540E;&#x9762;&#x76F4;&#x63A5;&#x8DDF;&#x51FD;&#x6570;&#x7684;&#x7B7E;&#x540D;&#xFF0C;&#x53E6;&#x4E00;&#x79CD;&#x53EF;&#x8BFB;&#x6027;&#x66F4;&#x597D;&#xFF0C;&#x63A8;&#x8350;&#x4F7F;&#x7528;</p>
<pre><code class="lang-dart"><span class="hljs-comment">// &#x516C;&#x5F0F;&#xFF1A;typedef &#x51FD;&#x6570;&#x7C7B;&#x578B;&#x522B;&#x540D; = &#x51FD;&#x6570;&#x7B7E;&#x540D;</span>
<span class="hljs-keyword">typedef</span> F = <span class="hljs-keyword">void</span> <span class="hljs-built_in">Function</span>(<span class="hljs-built_in">String</span>);
</code></pre>
<hr>
<p><strong>&#x516C;&#x4F17;&#x53F7;&#x201C;&#x7F16;&#x7A0B;&#x4E4B;&#x8DEF;&#x4ECE;0&#x5230;1&#x201D;</strong></p>
<p><img src="https://gitee.com/arcticfox1919/ImageHosting/raw/master/img/20190301102949549.jpg" alt="20190301102949549"> </p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; Arcticfox 2020 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x6587;&#x6863;&#x4FEE;&#x8BA2;&#x4E8E;&#xFF1A;
2021-03-18 13:33:13
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="简单语法.html" class="navigation navigation-prev " aria-label="Previous page: 简单语法">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="进阶篇.html" class="navigation navigation-next " aria-label="Next page: 进阶篇">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"函数","level":"1.2.3","depth":2,"next":{"title":"进阶篇","level":"1.3","depth":1,"path":"进阶篇.md","ref":"进阶篇.md","articles":[{"title":"面向对象","level":"1.3.1","depth":2,"path":"面向对象.md","ref":"面向对象.md","articles":[]},{"title":"泛型","level":"1.3.2","depth":2,"path":"泛型.md","ref":"泛型.md","articles":[]},{"title":"枚举","level":"1.3.3","depth":2,"path":"枚举.md","ref":"枚举.md","articles":[]},{"title":"库与导入","level":"1.3.4","depth":2,"path":"库与导入.md","ref":"库与导入.md","articles":[]},{"title":"异步编程","level":"1.3.5","depth":2,"path":"异步编程.md","ref":"异步编程.md","articles":[{"title":"Future","level":"1.3.5.1","depth":3,"path":"异步编程之Future.md","ref":"异步编程之Future.md","articles":[]},{"title":"Stream","level":"1.3.5.2","depth":3,"path":"异步编程之Stream.md","ref":"异步编程之Stream.md","articles":[]},{"title":"Isolate","level":"1.3.5.3","depth":3,"path":"异步编程之Isolate.md","ref":"异步编程之Isolate.md","articles":[]}]},{"title":"异常处理与 Zone","level":"1.3.6","depth":2,"path":"异常处理.md","ref":"异常处理.md","articles":[]},{"title":"生成器","level":"1.3.7","depth":2,"path":"生成器.md","ref":"生成器.md","articles":[]},{"title":"标准流与文件","level":"1.3.8","depth":2,"path":"标准流与文件.md","ref":"标准流与文件.md","articles":[]},{"title":"附：运算符","level":"1.3.9","depth":2,"path":"运算符.md","ref":"运算符.md","articles":[]}]},"previous":{"title":"简单语法","level":"1.2.2","depth":2,"path":"简单语法.md","ref":"简单语法.md","articles":[]},"dir":"ltr"},"config":{"plugins":["anchor-navigation-ex","expandable-chapters-small","-lunr","-search","search-pro","-sharing","github","expandable-chapters","pageview-count","theme-comscore","tbfed-pagefooter","popup","custom-favicon"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy Arcticfox 2020","modify_label":"文档修订于：","modify_format":"YYYY-MM-DD HH:mm:ss"},"github":{"url":"https://github.com/arcticfox1919"},"search-pro":{},"popup":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":true},"favicon":"logo.ico","theme-comscore":{},"pageview-count":{},"custom-favicon":{},"expandable-chapters-small":{},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"arcticfox","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Dart编程指南","language":"zh-hans","links":{"sidebar":{"主页":"http://www.bczl.xyz/"}},"gitbook":"*"},"file":{"path":"函数.md","mtime":"2021-03-18T05:33:13.851Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-10-18T14:58:03.414Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

